package com.exit66.jukebox.data;

/**
 * @author andyb
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.exit66.jukebox.Options;
import com.exit66.jukebox.util.Lock;
import com.exit66.jukebox.util.XmlSegment;

public class Artist extends BVDatabaseObject {
    
    private int 	_artistID = -1;
    private String	_artistName;
    private String	_artistOrderName;
    private static Lock _findSaveLock = new Lock();
    
    public int getArtistID() {
        
        return _artistID;
        
    }
    
    public void setArtistID(int newValue) {
        
        _artistID = newValue;
        
    }
    
    public String getArtistName() {
        
        return _artistName;
        
    }
    
    public void setArtistName(String newValue) {
        
        if (newValue != null)
            _artistName = newValue.trim();
        else
            _artistName = null;
        
    }
    
    public String getArtistOrderName() {
        
        return _artistOrderName;
        
    }
    
    public void setArtistOrderName(String newValue) {
        
        if (newValue != null)
            _artistOrderName = newValue.trim();
        else
            _artistOrderName = null;
        
    }
    
    public boolean fetch() {
        
        if (openConnection()) {
            
            ResultSet rs = retrieveData("SELECT * FROM artist WHERE artist_id = " + _artistID);
            
            try {
                
                if (rs != null && rs.next()) {
                    
                    _artistName = rs.getString("artist_name");
                    _artistOrderName = rs.getString("artist_order_name");
                    
                } else {
                    
                    _artistName = "";
                    _artistOrderName = "";
                    return false;
                    
                }
                
            } catch (SQLException e) {
                
                System.err.println(e);
                
            }
            
            closeConnection();
            
            return true;
            
        } else {
            
            return false;
            
        }
        
    }
    
    public synchronized void findAndSave() {
        _findSaveLock.lock();
        guessArtistOrderName();
        if (!find()) {            
            save();
        }
        _findSaveLock.releaseLock();
    }
    
    public void save() {
        
        ResultSet rs;
        
        guessArtistOrderName();
        
        if (openConnection()) {
            
            try {
                
                if (_artistID == -1) {
                    
                    this.find();
                    
                }
                
                if (_artistID == -1) {
                    
                    rs = retrieveData("INSERT INTO artist (artist_name, artist_order_name) VALUES (" +
                            Qts(_artistName) + ", " + Qts(_artistOrderName) + "); CALL IDENTITY()");
                    
                    try {
                        
                        if (rs.next()) {
                            
                            _artistID = rs.getInt(1);
                            
                        }
                        
                    } catch (SQLException e) {
                        
                        System.err.println(e);
                        
                    }
                    
                } else {
                    
                    executeStatement("UPDATE artist SET artist_name = " + Qts(_artistName) + ", " +
                            "artist_order_name = " + Qts(_artistOrderName) +
                            " WHERE artist_id = " + _artistID);
                    
                }
                
            } catch (Exception e) {
                
                System.err.println(e);
                
            }
            
            closeConnection();
            
        }
        
    }
    
    public void remove() {
        
        if (openConnection()) {
            
            executeStatement("DELETE FROM artist WHERE artist_id = " + _artistID);
            
            executeStatement("DELETE FROM track WHERE track_id = " + _artistID);
            
            executeStatement("DELETE FROM artistalbumlink WHERE album_id = " + _artistID);
            
            executeStatement("DELETE FROM album WHERE album_id NOT IN (SELECT album_id FROM artistalbumlink)");
            
            closeConnection();
        }
        
    }
    
    public boolean find() {
        
        openConnection();
        
        if ((_artistOrderName != null) && (_artistOrderName.length() == 0)) {
            guessArtistOrderName();
        }
        ResultSet rs = retrieveData("SELECT artist_id FROM artist " + 
            "WHERE artist_name = " + Qts(_artistName)  + " OR " + 
                "artist_order_name = " + Qts(_artistOrderName));
        
        try {
            
            if (rs != null && rs.next()) {
                
                closeConnection();
                _artistID = rs.getInt("artist_id");
                return true;
                
            }
            
        } catch (SQLException e) {
            
            System.err.println(e);
            
        }
        
        closeConnection();
        return false;
        
    }
    
    public Properties getProperties(Properties prop) {
        
        prop.setProperty("artist_id", String.valueOf(_artistID));
        prop.setProperty("artist_name", noNull(_artistName));
        prop.setProperty("artist_order_name", noNull(_artistOrderName));
        
        return prop;
        
    }
    
    public void guessArtistOrderName() {
        
        if ((_artistOrderName == null) || (_artistOrderName.length() == 0)) {
            
            _artistOrderName = _artistName;
            
            int firstSpace = _artistName.indexOf(" ");
            
            if (firstSpace != -1) {
                String firstWord = _artistName.substring(0, firstSpace).toLowerCase();
                String[] words = Options.getIgnoreAlpha();
                for (int i=0; i<words.length; i++) {
                    if (words[i].compareTo(firstWord) == 0) {
                        _artistOrderName = _artistName.substring(firstSpace + 1);
                    }
                }
            }
            
        }
    }
    
    public XmlSegment returnXml(boolean expandChildren) {
        XmlSegment xml = new XmlSegment();
        xml.startNode("artist");
        xml.writeNode("id", String.valueOf(_artistID));
        xml.writeNode("name", _artistName);
        xml.writeNode("orderName", _artistOrderName);
        xml.endNode("artist");
        
        return xml;
    }
    
}